import address from '@/utils/address'

const state = {
	token: '',
	remember: true,
	notice: false,
	comment: false,
	like: false,
	term: '',
	message: [],
}

const mutations = {
	SET_TOKEN: (state, data) => {
		state.token = data
	},
	SET_REMEMBER: (state, data) => {
		state.remember = data
	},
	SET_NOTICE: (state, data) => {
		state.notice = data
	},
	SET_COMMENT: (state, data) => {
		state.comment = data
	},
	SET_LIKE: (state, data) => {
		state.like = data
	},
	SET_TERM: (state, data) => {
		state.term = data
	},
	SET_MESSAGE: (state, data) => {
		const types = ['0', '1', '2']
		setTimeout(() => {
			if(types.includes(data.type)) {	// 互动消息
				const interact = data.type == '0' ? '关注了' : data.type == '1' ? '赞了' : '评论了'
				const index = state.message.findIndex(item => {
					return item.uid == 'sys'
				})
				if(index == -1) {
					state.message.push({
						uid: 'sys',
						avatar: '/static/sys_avatar.png',
						name: '互动消息',
						badge: 1,
						label: data.nickname + interact + '你',
						sort: state.message.length
					})
				}else {
					const badge = state.message[index].badge
					state.message[index].sort = state.message.length
					state.message[index].badge = badge + 1
					state.message[index].label = data.nickname + interact + '你'
				}
			}else if(data.type == '4') {		// 用户消息
				const index = state.message.findIndex(item => {
					return item.uid == data.from
				})
				if(index == -1) {
					state.message.push({
						uid: data.from,
						avatar: address.fileUrl + data.avatar,
						name: data.nickname,
						badge: 1,
						label: data.content,
						sort: state.message.length
					})
				}else {
					const badge = state.message[index].badge
					state.message[index].avatar = address.fileUrl + data.avatar
					state.message[index].name = data.nickname
					state.message[index].sort = state.message.length
					state.message[index].badge = badge + 1
					state.message[index].label = data.content
				}
			}
			state.message.sort((a, b) => {
				return b.sort - a.sort
			})
		})
	},
	CLEAR_BADGE: (state, data) => {
		const index = state.message.findIndex(item => {
			return item.uid == data
		})
		state.message[index].badge = 0
	},
	UPDATE_MESSAGE: (state, data) => {
		setTimeout(() => {
			state.message.map(item => {
				return item.badge = 0
			})
			if(data.notice.length > 0) {
				const notice = data.notice[0]
				const interact = notice.type == '0' ? '关注了' : data.type == '1' ? '赞了' : '评论了'
				const index = state.message.findIndex(item => {
					return item.uid == 'sys'
				})
				if(index == -1) {
					state.message.push({
						uid: 'sys',
						avatar: '/static/sys_avatar.png',
						name: '互动消息',
						badge: notice.badge,
						label: notice.nickname + interact + '你',
						sort: state.message.length
					})
				}else {
					state.message[index].sort = state.message.length
					state.message[index].badge = notice.badge
					state.message[index].label = notice.nickname + interact + '你'
				}
			}
			if(data.msg.length > 0) {
				console.log(data.msg);
				data.msg.forEach(msg => {
					const index = state.message.findIndex(item => {
						return item.uid == msg.source
					})
					if(index == -1) {
						state.message.push({
							uid: msg.source,
							avatar: address.fileUrl + msg.avatar,
							name: msg.nickname,
							badge: msg.badge,
							label: msg.label,
							sort: state.message.length
						})
					}else {
						state.message[index].avatar = address.fileUrl + msg.avatar
						state.message[index].name = msg.nickname
						state.message[index].sort = state.message.length
						state.message[index].badge = msg.badge
						state.message[index].label = msg.label
					}
				})
			}
			state.message.sort((a, b) => {
				return b.sort - a.sort
			})
		})
	}
}

const actions = {
	storeToken: ({ commit }, data) => {
		commit('SET_TOKEN', data)
	},
	storeSetting: ({ commit }, data) => {
		const {
			remember
		} = data
		commit('SET_REMEMBER', remember)
	},
	primarySet: ({ commit }, data) => {
		const [
			notice,
			comment,
			like,
		] = data
		commit('SET_NOTICE', notice)
		commit('SET_COMMENT', comment)
		commit('SET_LIKE', like)
	},
	storeTerm: ({ commit }, data) => {
		commit('SET_TERM', data)
	},
	newMessage: ({ commit }, data) => {
		commit('SET_MESSAGE', data)
	},
}

const common = {
	state,
	mutations,
	actions
}

export default common